package main

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/nsqio/go-nsq"
	"go-web/model"
	"io"
	"log"
	"math/rand"
	"os"
	"time"
)

var mq *nsq.Producer
var mq1 *nsq.Producer
var mq2 *nsq.Producer
var respChan = make(chan *nsq.ProducerTransaction, 1)

func init() {
	conn := "172.16.58.90:4150"
	config := nsq.NewConfig()
	var err error
	mq, err = nsq.NewProducer(conn, config)
	if err != nil {
		log.Panic(err)
	}

	conn1 := "172.16.58.89:4150"
	config1 := nsq.NewConfig()
	var err1 error
	mq1, err1 = nsq.NewProducer(conn1, config1)
	if err1 != nil {
		log.Panic(err1)
	}

	conn2 := "172.16.58.88:4150"
	config2 := nsq.NewConfig()
	var err2 error
	mq2, err2 = nsq.NewProducer(conn2, config2)
	if err2 != nil {
		log.Panic(err2)
	}
}


func main() {

	go func() {
		i := 0;
		for resp := range respChan {
			i++
			if i % 5000 == 0 {
				fmt.Println(resp)
			}
		}
	}()

	rand.Seed(time.Now().UnixNano())

	gin.SetMode(gin.ReleaseMode)
	f, _ := os.Create("gin.log")
	gin.DefaultWriter = io.MultiWriter(f)

	r := gin.Default()
	r.GET("/send1", func(c *gin.Context) {
		send("orderCreated")
		c.JSON(200, gin.H{
			"times": 1,
		})
	})

	r.GET("/send2", func(c *gin.Context) {
		send("orderCreated")
		send("orderCreated")
		c.JSON(200, gin.H{
			"times": 2,
		})
	})

	r.GET("/send2Topic", func(c *gin.Context) {
		send("orderCreated1")
		send("orderCreated2")
		c.JSON(200, gin.H{
			"times": 2,
		})
	})


	r.GET("/sendAsync", func(c *gin.Context) {
		sendAsync("orderCreated")
		c.JSON(200, gin.H{
			"times": 2,
		})
	})

	r.GET("/sendAsyncWait", func(c *gin.Context) {
		sendAsyncWait("orderCreated")
		c.JSON(200, gin.H{
			"times": 2,
		})
	})

	r.GET("/send2Host", func(c *gin.Context) {
		send2Host("orderCreated")
		c.JSON(200, gin.H{
			"times": 2,
		})
	})


	r.Run()
}

func send(topic string) {
	var orderMsg = new(model.OrderMsg)
	orderMsg.OrderId = 1
	orderMsg.GoodsId = 1
	orderMsg.Number = 1
	orderMsg.UserId = 1
	orderMsg.CreatedAt = time.Now()

	jsonBytes, err := json.Marshal(orderMsg)
	if err != nil {
		log.Fatal(err)
	}

	// 发消息
	err = mq.Publish(topic, jsonBytes)
	if err != nil {
		log.Panic(err)
	}
}

func sendAsync(topic string) {
	var orderMsg = new(model.OrderMsg)
	orderMsg.OrderId = 1
	orderMsg.GoodsId = 1
	orderMsg.Number = 1
	orderMsg.UserId = 1
	orderMsg.CreatedAt = time.Now()

	jsonBytes, err := json.Marshal(orderMsg)
	if err != nil {
		log.Fatal(err)
	}

	// 异步发送消息，不等待nsqd的响应
	err = mq.PublishAsync(topic, jsonBytes, nil)
	if err != nil {
		log.Panic(err)
	}
}

func sendAsyncWait(topic string) {
	var orderMsg = new(model.OrderMsg)
	orderMsg.OrderId = 1
	orderMsg.GoodsId = 1
	orderMsg.Number = 1
	orderMsg.UserId = 1
	orderMsg.CreatedAt = time.Now()

	jsonBytes, err := json.Marshal(orderMsg)
	if err != nil {
		log.Fatal(err)
	}

	// 异步发送消息，等待nsqd的响应
	err = mq.PublishAsync(topic, jsonBytes, respChan, "12345")
	if err != nil {
		log.Panic(err)
	}
}



func send2Host(topic string) {
	var orderMsg = new(model.OrderMsg)
	orderMsg.OrderId = 1
	orderMsg.GoodsId = 1
	orderMsg.Number = 1
	orderMsg.UserId = 1
	orderMsg.CreatedAt = time.Now()

	jsonBytes, err := json.Marshal(orderMsg)
	if err != nil {
		log.Fatal(err)
	}

	// 发消息
	var mqRand *nsq.Producer
	i := rand.Intn(3)
	if i == 0 {
		mqRand = mq
	} else {
		mqRand = mq1
	}
	err = mqRand.Publish(topic, jsonBytes)
	if err != nil {
		log.Panic(err)
	}
}

func send3Host(topic string) {
	var orderMsg = new(model.OrderMsg)
	orderMsg.OrderId = 1
	orderMsg.GoodsId = 1
	orderMsg.Number = 1
	orderMsg.UserId = 1
	orderMsg.CreatedAt = time.Now()

	jsonBytes, err := json.Marshal(orderMsg)
	if err != nil {
		log.Fatal(err)
	}

	// 发消息
	var mqRand *nsq.Producer
	i := rand.Intn(3)
	if i == 0 {
		mqRand = mq
	} else if i == 1 {
		mqRand = mq1
	} else {
		mqRand = mq2
	}
	err = mqRand.Publish(topic, jsonBytes)
	if err != nil {
		log.Panic(err)
	}
}

